package com.suxii.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;

public class LeftListView extends FrameLayout {
    private Context context;

    // 侧滑界面，消息列表
    private ViewGroup mainLayout;
    // 主界面布局，聊天窗口
    private ViewGroup secondLayout;
    // 侧滑界面宽度
    private int formLeftWidth = 400;  //px

    //滑动系数,越大距离越短
    private float radio = 0.5f;

    public boolean isOpen = false;

    //动画
    private ValueAnimator moveAnimator;
    private ValueAnimator scaleAnimator;

    public LeftListView(Context context) {
        this(context, null);
    }

    public LeftListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LeftListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;

    }


    //private View v1;
    //private View v2;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        ViewGroup.LayoutParams l2 = secondLayout.getLayoutParams();
        int w2 = MeasureSpec.makeMeasureSpec(l2.width, MeasureSpec.EXACTLY);
        int h2 = MeasureSpec.makeMeasureSpec(l2.height, MeasureSpec.EXACTLY);
        secondLayout.measure(w2, heightMeasureSpec);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        formLeftWidth = getMeasuredWidth() / 2;

        ViewGroup.LayoutParams l1 = mainLayout.getLayoutParams();
        int w1 = MeasureSpec.makeMeasureSpec(formLeftWidth, MeasureSpec.EXACTLY);
        int h1 = MeasureSpec.makeMeasureSpec(l1.height, MeasureSpec.EXACTLY);
        mainLayout.measure(w1, heightMeasureSpec);
        //Log.e("aa", getMeasuredWidth() + "ass");


    }


    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        super.onLayout(b, i, i1, i2, i3);

        int mw = mainLayout.getMeasuredWidth();
        int mh = mainLayout.getMeasuredHeight();
        //Log.e("aa", String.valueOf(mw));

        mainLayout.layout(0, 0, mw, mh);

        secondLayout.layout(0, 0, secondLayout.getMeasuredWidth(), secondLayout.getMeasuredHeight());


    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mainLayout = (ViewGroup) getChildAt(0);
        secondLayout = (ViewGroup) getChildAt(1);
    }


    private float startX, startY;
    private int move;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //int move = 0;
        if (MotionEvent.ACTION_DOWN == event.getAction()) {
            startX = event.getX();
            startY = event.getY();
            Log.e("aa", String.valueOf(startX));
        } else if (MotionEvent.ACTION_MOVE == event.getAction()) {
            float x = event.getX();

            if (!isOpen) {
                if (x - startX > 0) { //手指滑动的距离>0为右滑动
                    move = (int) (((x - startX) / radio) > formLeftWidth ? formLeftWidth : (x - startX) / radio);

                    secondLayout.setTranslationX(move);

                }
            } else {
                if (startX - x > 0) {
                    float v = ((startX - x) / radio) > formLeftWidth ? formLeftWidth : (startX - x) / radio;
                    Log.e("aa", String.valueOf(v));
                    secondLayout.setTranslationX((formLeftWidth - v));
                    move = (int) (formLeftWidth - v);
                }
            }


            //设置阴影动画，过度
            //secondLayout.setElevation((float) move/formLeftWidth*100);
            //mainLayout.setTranslationX(-move/10);
            //mainLayout.setTranslationY(move);
            //左边列表的移动
            //mainLayout.setTranslationX(move/10);

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (move > formLeftWidth / 1.5) { //滑动一半就为打开
                isOpen = true;
                //v2.setTranslationX(formLeftWidth);
                moveAnim(formLeftWidth);
            } else {
                isOpen = false;
                //v2.setTranslationX(0);
                moveAnim(0);
            }
            //左边列表的动画
            //scaleAnim((float) move/formLeftWidth);
            Log.e("aa", String.valueOf(isOpen));
        }

        return true;
    }

    // 为回弹添加动画
    private void moveAnim(float f) {
        if (f != 0) {
            moveAnimator = ValueAnimator.ofFloat(move, formLeftWidth);
            move = formLeftWidth;
        } else {
            moveAnimator = ValueAnimator.ofFloat(move, 0);
            move = 0;
        }
        //Log.e("aa","amin:"+move);
        moveAnimator.setDuration(100);
        moveAnimator.setInterpolator(new LinearInterpolator());
        moveAnimator.addUpdateListener(v -> {
            secondLayout.setTranslationX((Float) v.getAnimatedValue());
            //Log.e("aa", "amin"+ v.getAnimatedValue());
        });
        moveAnimator.start();
    }

    // 为回弹添加缩放动画
    private void scaleAnim(float f) {
        if (f > formLeftWidth/2) {
            scaleAnimator = ValueAnimator.ofFloat(f, formLeftWidth);
        } else {
            scaleAnimator = ValueAnimator.ofFloat(f, 0);
        }
        //Log.e("aa","amin:"+move);
        scaleAnimator.setDuration(100);
        scaleAnimator.setInterpolator(new LinearInterpolator());
        scaleAnimator.addUpdateListener(v -> {
            mainLayout.setTranslationX((Float) v.getAnimatedValue());
            //Log.e("aa",v.getAnimatedValue()+"ghj");
        });
        scaleAnimator.start();
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        //Log.e("aa","onInterceptTouchEvent"+super.onInterceptTouchEvent(ev));
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            float x = ev.getX();
            float y = ev.getY();
            if (!isOpen) {// 列表没有打开，左边区域可滑动
                return x < formLeftWidth && y <getMeasuredHeight()- getMeasuredHeight()/4 &&y>getMeasuredHeight()/4;
            } else {// 打开之后右边区域滑动
                return x > formLeftWidth;
            }
        }
        return false;
    }


    public void moveBackAnim() {
        if (isOpen) {
            moveAnimator = ValueAnimator.ofFloat(formLeftWidth,0);
        } else {
            moveAnimator = ValueAnimator.ofFloat(0,formLeftWidth);
        }
        isOpen= !isOpen;
        moveAnimator.setDuration(100);
        moveAnimator.setInterpolator(new LinearInterpolator());
        moveAnimator.addUpdateListener(v -> {
            secondLayout.setTranslationX((Float) v.getAnimatedValue());
            //Log.e("aa", "amin"+ v.getAnimatedValue());
        });
        moveAnimator.start();
    }


}
